



//
//  MemberViewController.m
//  ZQLearn
//
//  Created by 朵 on 2019/5/6.
//  Copyright © 2019 macfeel. All rights reserved.
//

#import "MemberViewController.h"
#import "NFourIconView.h"
#import "MemberIconView.h"
#import "MemberHeadView.h"
#import "MemberDayView.h"
#import "PayModel.h"
@interface MemberViewController ()<UITableViewDelegate,UITableViewDataSource,ZFActionSheetDelegate>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,strong)MemberDayView *dayView;

@property (nonatomic,strong)MemberIconView *iconView;

@property (nonatomic,strong)MemberHeadView *headView;

@property(nonatomic,copy)NSString *money;
@property(nonatomic,copy)NSString *day;


@property(nonatomic,copy)NSString *orderId;

@end
#define cellIdentifier @"mem"

@implementation MemberViewController
-(UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight -ScreenHeightFlex) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 160;
//        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
        _tableView.separatorStyle = 0;
//        [_tableView registerNib:[UINib nibWithNibName:@"NewsCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
       
//        WeakStament(ws);
//        _tableView.mj_header = [CustomRefreshHeader headerWithRefreshingBlock:^{
//            [ws refreshData];
//        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UserModel shareInstance].isMine = YES;
    
    
    if ([[UserModel shareInstance].isVip isEqualToNumber:@(1)]) {
        //是会员
        [UserInfoSave shareInstance].vipLevel = [[NSUserDefaults standardUserDefaults]objectForKey:@"vipLevel"];
        self.headView.vipTipLabel.text = [NSString stringWithFormat:@"您的VIP等级是:VIP%@",[UserInfoSave shareInstance].vipLevel?:@""];
        self.headView.bottomTipLab.hidden = YES;
        
    }else{
        //不是会员
        self.headView.vipTipLabel.text = [NSString stringWithFormat:@"您还不是VIP"];
        self.headView.bottomTipLab.hidden = false;
    }
    
}

-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    ///添加中间的collectionView
    MemberDayView *dayView = [[MemberDayView alloc]initWithFrame:CGRectMake(0,135, ScreenWidth, 160)];
    
    //    dayView.titleArray = @[@"30天",@"90天",@"365天",];
    if (self.dataArray.count>0) {
        dayView.dataArray = self.dataArray;
    }
    WeakStament(ws);
    dayView.block = ^(NSInteger index, CommonModel * _Nonnull model) {
        [ws payWithIndex:index withModel:model];
    };
//    dayView.block = ^(NSInteger index) {
//        YYLog(@"%ld",index);
//        [ws payWithIndex:index];
//    };
    //    dayView.titleArray = @[@[@"30天",@"￥29"],@[@"90天",@"￥86"],@[@"90天",@"￥300"],];
    [self.headView addSubview:dayView];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [UserModel shareInstance].isMine = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self.navigationItem setItemWithTitle:@"我的会员" textColor:kNavigationTitleColor fontSize:18 itemType:center];
//    [self showNoDataImageView:@"none" withTitle:@"暂无数据哦"];
    self.dataArray = [NSMutableArray array];
    [self vipOrderData];
    
    [self.view addSubview:self.tableView];
    
    
    MemberHeadView *head = [[[NSBundle mainBundle]loadNibNamed:@"MemberHeadView" owner:self options:nil]lastObject];
    
    head.nameLab.text = [UserInfoSave shareInstance].nickName?:@"";

    [head.openBtn addTarget:self action:@selector(openAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.headView = head;
    
    self.tableView.tableHeaderView = head;
    
//    MemberDayView *dayView = [[MemberDayView alloc]initWithFrame:CGRectMake(0,135, ScreenWidth, 160)];
//
//    dayView.titleArray = @[@"30天",@"90天",@"365天",];
//    WeakStament(ws);
//    dayView.block = ^(NSInteger index) {
//        YYLog(@"%ld",index);
//        [ws payWithIndex:index];
//    };
////    dayView.titleArray = @[@[@"30天",@"￥29"],@[@"90天",@"￥86"],@[@"90天",@"￥300"],];
//    [head addSubview:dayView];
    
    
    // Do any additional setup after loading the view.
}


///获取vip 订单种类数据
-(void)vipOrderData{
    
    [ZNetworkService getRequestWithBaseUrl:kVipOrderUrl setParameters:@{} success:^(id result) {
        
        if ([result[@"errorCode"]integerValue]==0) {
            
            NSArray *arr = result[@"result"];
            for (NSDictionary *dict in arr) {
                CommonModel *model = [CommonModel mj_objectWithKeyValues:dict];
                [self.dataArray addObject:model];
            }
            
        }
        
        
    } fail:^(NSError *error) {
        
    } delegater:nil];
}

///点击开通
-(void)openAction:(UIButton *)btn
{
    
    //
    if ((!self.money)||(!self.day)) {
        
        [AFToast showText:@"请选择套餐"];
        return;
        
    }
    
    [self addVipOrderWithMoney:self.money day:self.day];
    
}

-(void)payWithIndex:(NSInteger)index withModel:(CommonModel *)model{
    
//    NSInteger money = 0;
//    NSInteger day = 0;
    
    self.money = [NSString stringWithFormat:@"%@",model.preferentialMoney];
    self.day = [NSString stringWithFormat:@"%@",model.vipDays];
    YYLog(@"%@%@",self.money,self.day);
//    switch (index) {
//        case 0:
//        {
//            money = 29;
//            day = 30;
//            self.money = [NSString stringWithFormat:@"%ld",money];
//            self.day = [NSString stringWithFormat:@"%ld",day];
////            [self addVipOrderWithMoney:money day:day];
//        }
//            break;
//        case 1:
//        {
//            money = 86;
//            day = 90;
//            self.money = [NSString stringWithFormat:@"%ld",money];
//            self.day = [NSString stringWithFormat:@"%ld",day];
////            [self addVipOrderWithMoney:money day:day];
//        }
//            break;
//        case 2:
//        {
//            money = 300;
//            day = 365;
//            self.money = [NSString stringWithFormat:@"%ld",money];
//            self.day = [NSString stringWithFormat:@"%ld",day];
////            [self addVipOrderWithMoney:money day:day];
//        }
//            break;
//
//        default:
//            break;
//    }
}

///添加vip订单
-(void)addVipOrderWithMoney:(NSString *)money day:(NSString *)day{
    
    NSDictionary *dict = @{@"payMoney":money,@"vipDays":day,@"userInfo.id":[UserInfoSave shareInstance].uid?:@"",};
    [ZNetworkService postRequestWithBaseUrl:kAddVipOrderUrl setParameters:dict success:^(id result) {
        YYLog(@"%@",result);
        if ([result[@"errorCode"]integerValue]==0) {
            NSString *orderId = result[@"result"][@"id"];
            self.orderId = orderId;
            ZFActionSheet *sheet = [ZFActionSheet actionSheetWithTitle:nil confirms:@[@"微信支付",] cancel:@"取消" style:ZFActionSheetStyleDefault];
            sheet.delegate = self;
            [sheet show];
        }
    } fail:^(NSError *error) {
        
    } delegater:nil];
}

#pragma mark - zfdelegate
-(void)clickAction:(ZFActionSheet *)actionSheet atIndex:(NSUInteger)index{
    if (index==0) {
        //微信支付
        //type  1 商城里的商品  2 vip   3 图书视频等资源
        //paytype 1支付宝  2微信
        ///type 1商城商品  2vip  3电子书资源
        NSDictionary *dict = @{@"id":self.orderId?:@"",@"type":@"2",@"paytype":@"2",};
        [ZNetworkService postRequestWithBaseUrl:kCallPayUrl setParameters:dict success:^(id result) {
            YYLog(@"%@",result);
            if ([result[@"errorCode"]integerValue]==0) {
                NSDictionary *ddic = result[@"result"];
                PayModel *model = [PayModel mj_objectWithKeyValues:ddic];
                [PayModel  wxPay:model];
            }else{
                [AFToast showText:@"接口返回错误"];
            }
        } fail:^(NSError *error) {
            
        } delegater:nil];
        
    }else{
        
        //支付宝
    }
}
#pragma mark - datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return 1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell addSubview:self.iconView];
    return cell;
}



-(MemberIconView *)iconView{
    
    if (!_iconView) {
        _iconView = [[MemberIconView alloc]initWithFrame:CGRectMake(0,20, ScreenWidth, 135)];
        _iconView.iconArray = @[@"icon_read",@"icon_double",@"icon_discount",@"icon_shengji",@"icon_mark",@"icon_kefu",@"icon_gift",@"icon_book",];
        _iconView.titleArray = @[@"海量阅读",@"双倍积分",@"购物折扣",@"飞速升级",@"尊贵标识",@"专属客服",@"等级礼包",@"原创新书"];
        
       _iconView.indexBlock = ^(NSInteger indexNum) {
            YYLog(@"%ld",indexNum);

        };
    }
    return _iconView;
}


@end
